package com.platform.file.service.impl;

import com.platform.common.core.exception.ServiceException;
import com.platform.file.config.MinioConfig;
import com.platform.file.service.ISysFileService;
import com.platform.file.utils.FileUploadUtils;
import com.platform.file.utils.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * Minio 文件存储
 *
 * @author mbtec
 */
@Slf4j
@Service
@Primary
public class MinioSysFileServiceImpl implements ISysFileService {
    @Autowired
    private MinioConfig minioConfig;
    //    MinioUtils minioUtils=new MinioUtils(minioConfig.getUrl(),minioConfig.getBucketName(), minioConfig.getAccessKey(),minioConfig.getSecretKey());
    @Autowired
    MinioUtils minioUtils;

    /**
     * 文件上传接口（根据文件类型上传）
     *
     * @param file            上传的文件
     * @param cataloguePrefix 目录前缀
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String upload(MultipartFile file, String serviceName, String cataloguePrefix) throws Exception {


        //文件名称
        String originalFilename = FileUploadUtils.extractFilename(file);
        String filePath = "";
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            filePath = cataloguePrefix + "/" + serviceName + "/" + originalFilename;
            minioUtils.putObject(null, file, filePath, file.getContentType());
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";

        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }




    /**
     * 文件上传接口（根据文件类型上传）
     *
     * @param file            上传的文件
     * @param cataloguePrefix 目录前缀
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadByBucketName(MultipartFile file, String serviceName, String cataloguePrefix, String bucketName) throws Exception {
        //文件名称
        String originalFilename = FileUploadUtils.extractFilename(file);
        String filePath = "";
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            filePath = cataloguePrefix + "/" + serviceName + "/" + originalFilename;
            minioUtils.putObject(bucketName, file, filePath, file.getContentType());
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";

        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

    @Override
    public String uploadByBase64(String fileName,InputStream inputStream, String serviceName, String cataloguePrefix, String bucketName) throws Exception {
        //文件名称
        String filePath = "";
        try {
            filePath = cataloguePrefix + "/" + serviceName + "/" + fileName;
            minioUtils.putObject(bucketName,filePath,inputStream);
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";

        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }    }


    /**
     * 上传文件(自定义文件名)
     *
     * @param inputStream 上传的文件
     * @param filePath    文件路径
     * @param contentType
     * @return
     * @throws Exception
     */
    @Override
    public String uploadByAsyncFileName(InputStream inputStream, String filePath, String contentType) throws Exception {
        try {
            minioUtils.putObject(null, filePath, inputStream, contentType);
            inputStream.close();
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";

        }
    }

    /**
     * 上传文件(自定义文件名)
     *
     * @param inputStream 上传的文件
     * @param filePath    文件路径
     * @param contentType
     * @param bucketName
     * @return
     * @throws Exception
     */
    @Override
    public String uploadByAsyncFileNameAndBucketName(InputStream inputStream, String filePath, String contentType, String bucketName) throws Exception {
        try {
            minioUtils.putObject(bucketName, filePath, inputStream, contentType);
            inputStream.close();
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";

        }
    }

    /**
     * 获取文件流
     *
     * @param filePath
     * @return
     * @throws Exception
     */
    @Override
    public InputStream downLoadFile(String filePath) throws Exception {
        //判断文件是否存在
        if (minioUtils.doesObjectExist(null, filePath)) {
            return minioUtils.getObject(null, filePath);
        } else {
            return null;
        }
    }

    /**
     * 上传文件
     *
     * @param filePath
     * @param inputStream
     * @param contentType
     * @return
     * @throws Exception
     */
    @Override
    public String uploadFile(String filePath, InputStream inputStream, String contentType) throws Exception {
        if (contentType != null) {
            minioUtils.putObject(null, filePath, inputStream, contentType);
        } else {
            minioUtils.putObject(null, filePath, inputStream);
        }

        return filePath;
    }

    @Override
    public String uploadShareFile(MultipartFile file, String filePath) throws Exception {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            minioUtils.putObject(null, file, filePath, file.getContentType());
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

    /**
     * 删除文件
     *
     * @param filePath
     * @throws Exception
     */
    @Override
    public void deleteFile(String filePath) throws Exception {
        //判断文件是否存在
        if (minioUtils.doesObjectExist(null, filePath)) {
            minioUtils.removeObject(null, filePath);
        }

    }

    /**
     * 获取文件流
     *
     * @param filePath
     * @return
     * @throws Exception
     */
    @Override
    public InputStream downLoadFileNew(String bucketName, String filePath) throws Exception {
        //判断文件是否存在
        if (minioUtils.doesObjectExist(bucketName, filePath)) {
            return minioUtils.getObject(bucketName, filePath);
        } else {
            return null;
        }
    }
}
